package entity

import (
	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//XFFZSDR 新发放中收导入
type XFFZSDR struct {
	ID   int     `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	KHYF int     `gorm:"column:KHYF"`
	XH   int     `gorm:"column:XH"`
	KMMC string  `gorm:"column:KMMC;size:100"`
	KHMC string  `gorm:"column:KHMC;size:100"`
	ZSJE float64 `gorm:"column:ZSJE"`
	ZSNX float64 `gorm:"column:ZSNX"`
	ZSFL float64 `gorm:"column:ZSFL"`
	DRJG string  `gorm:"column:DRJG;"`
}

//XffzsBeforeImportValidation 导入前执行，验证数据
func XffzsBeforeImportValidation(index int, rowdatas []string) []base.ErrorData {
	sql := "SELECT ID FROM T_COMP_CLIE WHERE CUNM=?"
	rows, _ := base.DB.Raw(sql, rowdatas[1]).Rows()
	var CUID int
	if rows.Next() {
		rows.Scan(&CUID)
	}
	errDatas := make([]base.ErrorData, 1, 1)
	if CUID == 0 {
		errInfos := make([]base.ErrorInfo, 1, 1)
		errinfo := base.ErrorInfo{
			ColIndex: 1,
			Message:  "客户名称错误错误或不存在！",
		}
		errInfos[0] = errinfo
		errData := base.ErrorData{
			RowIndex:   index,
			DataValues: rowdatas,
			ErrorInfos: errInfos,
		}
		errDatas[0] = errData
		return errDatas
	}
	return nil
}

//XffzsAfterImport 导入后执行
func XffzsAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	// err = tx.Exec("UPDATE T_BASE_YXFY_XFFZS SET CUID=B.ID "+
	// 	"FROM T_BASE_YXFY_XFFZS A INNER JOIN T_COMP_CLIE B ON  RTRIM(LTRIM(A.KHMC)) = B.CUNM "+
	// 	"WHERE KHYF=?", params["KHYF"]).Error
	// if err != nil {
	// 	return err
	// }
	prams := make([]interface{}, 1)
	prams[0] = params["KHYF"]
	tx.Exec("EXEC UP_BASE_YXFY_XFFZS_DTIP ?", prams...)
	return nil
}

//TableName 新发放中收导入表
func (XFFZSDR) TableName() string {
	return "T_BASE_YXFY_XFFZS_DTIP"
}

//XFFZSDREF 返回结构体
func XFFZSDREF() base.EntityFunc {
	return func() interface{} {
		entity := XFFZSDR{}
		return &entity
	}
}

//XFFZSDRESF 返回结构体数组
func XFFZSDRESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []XFFZSDR
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("XFFZSDR", base.NewDefaultDao(XFFZSDREF(), XFFZSDRESF(), nil))
	//base.SetValidationCallBack("XFFZSDR", XffzsBeforeImportValidation)
	base.SetAfterCallBack("XFFZSDR", XffzsAfterImport)
}
